// pages/page/orderdetails/orderdetails.js
const app = getApp();
var request_api = require('../../../utils/require_api.js')
import Dialog from '../../../youzan/youzan_dist/dialog/dialog';
import Toast from '../../../youzan/youzan_dist/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    index_no: null,
    setorderdata: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    this.update_status(); //更新界面
  },

  //跳转到订单详情
  togoorderdetails(res) {
    wx.navigateTo({
      url: '../orderpage/orderpage?order_item=' + res.detail.orderid,
    })
  },

  // 收货成功！！！
  confirm_rece(res) {
    var that = this;
    var timestamp = Date.parse(new Date());
    var time = that.format_time(timestamp);
    Toast.loading({
      duration: 2000, // 持续展示 toast
      forbidClick: true, // 禁用背景点击
      message: '加载中......',
      loadingType: 'spinner',
    });
    request_api.request(
      'update_closetime', {
        close_time: time,
        orderId: res.detail.orderId,
      },
      function (res) {
        console.log(res, "我是修改结束时间");
      },
      function () {
        console.log("錯誤");
      },
    )
    //修改订单状态
    setTimeout(function() {
      request_api.request(
        'update_orderstatus', //需要传给wxrequest.js 的 url
        {
          "orderId": res.detail.orderId,
          "orderstatus": res.detail.orderstatus
        }, //需要传的 data
        function(res) {
          Toast.success('收货成功！！！');
          that.update_status(); //更新界面状态
        },
        function() {
          console.log("錯誤");
        },
      )
    }, 2500);
  },
  //付款
  paymoney(res) {
    var that = this;
    var orderid = res.detail.orderId;
    if (that.data.money < res.detail.totalmoney) {
      Dialog.alert({
        title: '余额不足',
        message: '您的余额不足，请前往充值页面进行充值！！'
      }).then(() => {
        wx.redirectTo({
          url: '../my_wallet/my_wallet',
        })
      });
      // 把订单变成为支付状态，，生成订单
      // 更改库存数量
      // 删除购物车表
    } else {
      request_api.request( //成功支付减少用户金额
        'update_del_user', {
          "balance": res.detail.totalmoney,
          "openid": app.globalData.openid
        }, //需要传的 data
        function(res) {
          //增加一条通知信息
          request_api.request(
            'insertannouncement', //需要传给wxrequest.js 的 url
            {
              "title": "订单支付成功通知",
              "content": "恭喜！您的订单已成功支付，详情请到我的订单查看！",
              "user_id": app.globalData.openid,
              "if_read": 0,
            }, //需要传的 data
            function(res) {
              console.log(res, "充值通知插入数据库成功,orderdetails")
            },
            function() {
              console.log("錯誤");
            },
          )


          //修改订单状态
          request_api.request(
            'update_orderstatus', //需要传给wxrequest.js 的 url
            {
              "orderId": orderid,
              "orderstatus": '1'
            }, //需要传的 data
            function(res) {
              Toast.loading({
                duration: 2000, // 持续展示 toast
                forbidClick: true, // 禁用背景点击
                message: '加载中......',
                loadingType: 'spinner',
              });
              setTimeout(function() {
                Toast.success('成功付款！！！');
                that.update_status(); //更新界面状态
              }, 2500);
              //增加销量
              //这里是获取到某条订单下面的款有哪些
              request_api.request(
                'getorderbyoid', {
                  orderid: orderid
                },
                function(res) {
                  let style_id = '';
                  let Quantity = '';
                  for (let i = 0; i < res.data.length; i++) {
                    style_id = style_id + res.data[i].goods_id + ',';
                    Quantity = Quantity + res.data[i].num + ',';
                  }
                  request_api.request(
                    'update_xiaoliang', //需要传给wxrequest.js 的 url
                    {
                      "style_id": style_id,
                      "Quantity": Quantity
                    }, //需要传的 data
                    function(res) {},
                    function() {
                      console.log("錯誤");
                    },
                  )
                },
                function() {
                  console.log("錯誤");
                },
              )
            },
            function() {
              console.log("錯誤");
            },
          )
        }
      )
    }
  },

  //父组件接收子组件传值    取消订单
  compontpass: function(res) {
    console.log(res,"我要取消订单");
    var orde = res.detail.orderId;
    var that = this;
    request_api.request(
      'getorderbyoid', {
        orderid: res.detail.orderId
      },
      function(res) {
        console.log(res, "我是取缔的人");
        let goodsid = [];
        let colorid = [];
        let filed = [];
        let num = [];
        for (let i = 0; i < res.data.length; i++) {
          goodsid.push(res.data[i].goods_id);
          colorid.push(res.data[i].ColorID);
          filed.push(res.data[i].Filed);
          num.push(res.data[i].num);
        }
      request_api.request( //增加库存数量
      'update_f002quajia', {
        quatity: num,
        goodsid: goodsid,
        colorid: colorid,
        Filed: filed
      },
      function (res) {
        console.log(res, "我是增加的库存");
      },
      function () {
        console.log("錯誤");
      },
    )
      var timestamp = Date.parse(new Date());
       var time= that.format_time(timestamp);
        request_api.request( //增加库存数量
          'update_closetime', {
            close_time: time,
            orderId: orde,
          },
          function (res) {
            console.log(res, "我是修改结束时间");
          },
          function () {
            console.log("錯誤");
          },
        )


      },
      function() {
        console.log("錯誤");
      },
    )

    


    request_api.request(
      'update_orderstatus', //需要传给wxrequest.js 的 url
      {
        "orderId": res.detail.orderId,
        "orderstatus": res.detail.orderstatus
      }, //需要传的 data
      function(res) {
        Toast.success('成功取消订单！！！');
        that.update_status(); //更新界面状态
      },
      function() {
        console.log("錯誤");
      },
    )

    // if (res.detail) {
    //   request_api.request(
    //     'update_orderstatus', //需要传给wxrequest.js 的 url
    //     {
    //       "orderId": res.detail.orderId,
    //       "orderstatus": res.detail.orderstatus
    //     }, //需要传的 data
    //     function(res) {
    //       that.update_status(); //更新界面状态
    //     },
    //     function() {
    //       console.log("錯誤");
    //     },
    //   )


    // }
  },
  //修改状态
  update_status() { //更新状态信息
    var that = this;
    request_api.request( //这个接口是获取到某一个用户下的所有订单信息，根据获取的信息状态来分类
      'getordersitem', {
        userid: app.globalData.openid
      },
      function(res) {
        var arr = res.data;
        var ordershowlist = [];
        var totalnum = 0;
        for (var index in arr) {
          ordershowlist.push({
            orderId: index,
            orderstatus: arr[index][0].orderstatus,
            create_time: arr[index][0].create_time,
            data: arr[index],
            totalmoney: arr[index][0].totalmoney
          })
        }
        for (let i = 0; i < ordershowlist.length; i++) {
          totalnum = 0;
          for (let j = 0; j < ordershowlist[i].data.length; j++) {
            totalnum = totalnum + ordershowlist[i].data[j].num;
          }
          ordershowlist[i].totalnum = totalnum;
        }

        for (let i = 0; i < ordershowlist.length; i++) {
          if (ordershowlist[i].orderstatus == 0) {
            ordershowlist[i].orderstatus1 = '未付款';
          } else if (ordershowlist[i].orderstatus == 1) {
            ordershowlist[i].orderstatus1 = '待发货';
          } else if (ordershowlist[i].orderstatus == 2) {
            ordershowlist[i].orderstatus1 = '已发货';
          } else if (ordershowlist[i].orderstatus == 3) {
            ordershowlist[i].orderstatus1 = '交易成功';
          } else if (ordershowlist[i].orderstatus == 4) {
            ordershowlist[i].orderstatus1 = '交易关闭';
          }
        }
        that.data.setorderdata = ordershowlist;
        let setordata = [];
        let setordata0 = [];
        let setordata1 = [];
        let setordata2 = [];
        let setordata3 = [];
        let setordata4 = [];
        for (let i = 0; i < that.data.setorderdata.length; i++) {
          if (that.data.active == 0) {
            setordata0 = that.data.setorderdata; //全部
          } else if (that.data.setorderdata[i].orderstatus == 0) {
            setordata1.push(that.data.setorderdata[i]); //待付款
          } else if (that.data.setorderdata[i].orderstatus == 1) {
            setordata2.push(that.data.setorderdata[i]); //代发货
          } else if (that.data.setorderdata[i].orderstatus == 2) {
            setordata3.push(that.data.setorderdata[i]); //待收货
          } else if (that.data.setorderdata[i].orderstatus == 3) {
            setordata4.push(that.data.setorderdata[i]); //交易成功
          }
        }
        if (that.data.active == 0) {
          setordata = setordata0;
        } else if (that.data.active == 1) {
          setordata = setordata1;
        } else if (that.data.active == 2) {
          setordata = setordata2;
        } else if (that.data.active == 3) {
          setordata = setordata3;
        } else if (that.data.active == 4) {
          setordata = setordata4;
        }
        that.setData({
          active: that.data.active,
          ordershowlist: setordata
        })
      },
      function() {
        console.log("錯誤");
      },
    )
  },
  format_time(time) {
    var date = new Date(time);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = date.getDate() + ' ';
    let h = date.getHours() + ':';
    let m = date.getMinutes() + ':';
    let s = date.getSeconds();
    return Y + M + D + h + m + s;
    console.log(Y + M + D + h + m + s);
  },
  onChange(e) {
    this.data.index_no = e.detail.index;
    let setordata = [];
    let setordata0 = [];
    let setordata1 = [];
    let setordata2 = [];
    let setordata3 = [];
    let setordata4 = [];
    for (let i = 0; i < this.data.setorderdata.length; i++) {
      if (e.detail.index == 0) {
        setordata0 = this.data.setorderdata; //全部
      } else if (this.data.setorderdata[i].orderstatus == 0) {
        setordata1.push(this.data.setorderdata[i]); //待付款
      } else if (this.data.setorderdata[i].orderstatus == 1) {
        setordata2.push(this.data.setorderdata[i]); //代发货
      } else if (this.data.setorderdata[i].orderstatus == 2) {
        setordata3.push(this.data.setorderdata[i]); //待收货
      } else if (this.data.setorderdata[i].orderstatus == 3) {
        setordata4.push(this.data.setorderdata[i]); //待收货
      }
    }

    if (e.detail.index == 0) {
      setordata = setordata0;
    } else if (e.detail.index == 1) {
      setordata = setordata1;
    } else if (e.detail.index == 2) {
      setordata = setordata2;
    } else if (e.detail.index == 3) {
      setordata = setordata3;
    } else if (e.detail.index == 4) {
      setordata = setordata4;
    }

    this.setData({
      active: e.detail.index,
      ordershowlist: setordata
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function(options) {
    console.log(options, "activity");
    if (options.activity != undefined) {
      this.data.active = options.activity;
    }
    //获取balance
    var that = this;
    request_api.request(
      'select_openiduser', //需要传给wxrequest.js 的 url
      {
        "openid": app.globalData.openid
      }, //需要传的 data
      function(res) {
        that.data.money = res.data.balance;
        console.log(that.data.money, "that.data.money");
      },
      function() {
        console.log("錯誤");
      },
    )
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})